/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.stop.risk.data.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.stop.risk.base.entity.LinePollMonitorFactorEntity;
import com.hopes.bizz.stop.risk.base.service.LinePollMonitorFactorService;
import com.hopes.bizz.stop.risk.data.dto.LineOmRtDataHisDTO;
import com.hopes.bizz.stop.risk.data.entity.LineOmRtDataHisEntity;
import com.hopes.bizz.stop.risk.data.mapper.LineOmRtDataHisMapper;
import com.hopes.bizz.stop.risk.data.service.LineOmRtDataHisService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 实时监控数据历史
 *
 * @author pig
 * @date 2024-02-22 11:20:58
 */
@Service
@RequiredArgsConstructor
public class LineOmRtDataHisServiceImpl extends ServiceImpl<LineOmRtDataHisMapper, LineOmRtDataHisEntity> implements LineOmRtDataHisService {

    private final LinePollMonitorFactorService linePollMonitorFactorService;

    @Override
    public JSONArray rtDataHis(LineOmRtDataHisDTO lineOmRtDataHisDTO) {
        JSONArray result = new JSONArray();
        List<Long> monitorFactorIds = lineOmRtDataHisDTO.getMonitorFactorIds();
        if (CollUtil.isNotEmpty(monitorFactorIds)) {
            for (Long monitorFactorId : monitorFactorIds) {
                LinePollMonitorFactorEntity pollMonitorFactor = linePollMonitorFactorService.getById(monitorFactorId);
                if (null == pollMonitorFactor || null == pollMonitorFactor.getFactorId() || null == pollMonitorFactor.getMonitorSiteId()) {
                    continue;
                }
                Long factorId = pollMonitorFactor.getFactorId();
                Long monitorSiteId = pollMonitorFactor.getMonitorSiteId();

                JSONObject monitorFactor = new JSONObject();
                final JSONArray timeArray = new JSONArray();
                final JSONArray dataArray = new JSONArray();
                List<LineOmRtDataHisEntity> lineOmRtDataHiss = lambdaQuery()
                        .eq(ObjectUtil.isNotNull(factorId), LineOmRtDataHisEntity::getFactorId, factorId)
                        .eq(ObjectUtil.isNotNull(monitorSiteId), LineOmRtDataHisEntity::getMonitorSiteId, monitorSiteId)
                        .ge(ObjectUtil.isNotNull(lineOmRtDataHisDTO.getBeginTime()), LineOmRtDataHisEntity::getDataTime, lineOmRtDataHisDTO.getBeginTime())
                        .le(ObjectUtil.isNotNull(lineOmRtDataHisDTO.getEndTime()), LineOmRtDataHisEntity::getDataTime, lineOmRtDataHisDTO.getEndTime())
                        .list();
                if (CollUtil.isEmpty(lineOmRtDataHiss)){
                    continue;
                }
                final List<LineOmRtDataHisEntity> lineOmRtDataHisList = lineOmRtDataHiss.stream().sorted(Comparator.comparing(LineOmRtDataHisEntity::getDataTime)).collect(Collectors.toList());
                for (LineOmRtDataHisEntity lineOmRtDataHis : lineOmRtDataHisList) {
                    timeArray.add(lineOmRtDataHis.getDataTime());
                    dataArray.add(lineOmRtDataHis.getRtd());
                }
                monitorFactor.put("timeArray", timeArray);
                monitorFactor.put("dataArray", dataArray);
                monitorFactor.put("monitorFactor", pollMonitorFactor);
                result.add(monitorFactor);
            }
        }
        return result;
    }

    @Override
    public Page<LineOmRtDataHisEntity> queryForListHisTable( Page page, LineOmRtDataHisDTO lineOmRtDataHis) {
        List<Long> monitorFactorIds = lineOmRtDataHis.getMonitorFactorIds();
        List<Long> factorIdList = linePollMonitorFactorService.lambdaQuery()
                .in(CollUtil.isNotEmpty(monitorFactorIds), LinePollMonitorFactorEntity::getId, monitorFactorIds).list()
                .stream().map(LinePollMonitorFactorEntity::getFactorId).collect(Collectors.toList());
        LambdaQueryWrapper<LineOmRtDataHisEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(factorIdList),LineOmRtDataHisEntity::getFactorId,factorIdList)
                .ge(ObjectUtil.isNotNull(lineOmRtDataHis.getBeginTime()), LineOmRtDataHisEntity::getDataTime, lineOmRtDataHis.getBeginTime())
                .le(ObjectUtil.isNotNull(lineOmRtDataHis.getEndTime()), LineOmRtDataHisEntity::getDataTime, lineOmRtDataHis.getEndTime());
        return page(page, wrapper);
    }
}
